﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.auth.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.auth.dao.*;
import com.hh.bss.auth.entity.*;
import com.hh.bss.auth.service.*;
import com.hh.bss.auth.vo.query.*;
import com.hh.bss.base.*;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.cache.SystemRoleResourceCache;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.common.tree.CompanyDeptTreeNode;
import com.hh.bss.common.tree.RoleResourceTreeNode;
import com.hh.bss.util.*;




@Service
@Transactional
public class SystemRoleServiceImpl extends BaseBusinessService<SystemRole,java.lang.Integer>  implements SystemRoleService<SystemRole,java.lang.Integer>{

	private SystemRoleDao systemRoleDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setSystemRoleDaoImpl(SystemRoleDao dao) {
		this.systemRoleDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.systemRoleDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(SystemRoleQuery query) {
		return systemRoleDao.findPage(query);
	}
	public void removeById(Integer id){
		List values = new ArrayList();
		values.add(id);
		List cclist=this.getEntityDao().getJdbcDao().findByQuery("select 1 from system_user_roles a where a.role_id=? ", values.toArray(), new MapObjectRowMapper());
		if (cclist!=null&&cclist.size()>0) {
			 throw new RuntimeException("角色已被使用，不能删除");
		}
		systemRoleDao.getHibernateTemplate().bulkUpdate("delete  from SystemRoleAcl t where t.roleId=? ", id);
		systemRoleDao.flush();
		systemRoleDao.deleteById(id);
	}
	public List getRoleResource(Integer roleId) {
		// 
		
		//查询所有的模块
		List moduleList=systemRoleDao.findByHql("from SystemModule t order by t.displayLevel");
		SystemModule systemModule=null;
		Map allSystemModuleTreeNode=new LinkedHashMap();
		Map allSystemModuleMap=new LinkedHashMap();
		RoleResourceTreeNode roleResourceTreeNode=null;
		 for(int i=0;moduleList!=null&&i<moduleList.size();i++){
			 systemModule=(SystemModule)moduleList.get(i);
			 roleResourceTreeNode=new RoleResourceTreeNode();
			 roleResourceTreeNode.setAuthNodeType(RoleResourceTreeNode.NodeType_module);
			 roleResourceTreeNode.setId("m"+systemModule.getId());
			 roleResourceTreeNode.setText(systemModule.getName());
			 roleResourceTreeNode.setParentId(ObjectUtils.toString(systemModule.getParentId()));
			 allSystemModuleTreeNode.put(systemModule.getId(), roleResourceTreeNode);
			 allSystemModuleMap.put(systemModule.getId(), systemModule);
			 //RoleResourceTreeNode.
		 }
		 
		 //子模块集合
//		 Map allSystemModuleChildTreeNode=new LinkedHashMap();
//
//			 for(int i=0;moduleList!=null&&i<moduleList.size();i++){
//				 systemModule=(SystemModule)moduleList.get(i);
//				 roleResourceTreeNode=new RoleResourceTreeNode();
//				 roleResourceTreeNode.setAuthNodeType(RoleResourceTreeNode.NodeType_module);
//				 roleResourceTreeNode.setId("m"+systemModule.getId());
//				 roleResourceTreeNode.setText(systemModule.getName());
//				 roleResourceTreeNode.setParentId(ObjectUtils.toString(systemModule.getParentId()));
//				 Set childSet=null;
//				 if (systemModule.getParentId()!=null&&systemModule.getParentId().intValue()!=0){
//					 childSet=(Set)allSystemModuleChildTreeNode.get(systemModule.getParentId());
//					 if (childSet==null) {
//						 childSet=new HashSet();
//						 allSystemModuleChildTreeNode.put(systemModule.getParentId(), childSet);
//					 }
//					 childSet.add(roleResourceTreeNode);	 
//				 }				 
//				 //RoleResourceTreeNode.
//			 }
		 
		//设置子模块
		 RoleResourceTreeNode systemModuleTreeNode=null;
		 RoleResourceTreeNode parentSystemModuleTreeNode=null;
		 for(int i=0;moduleList!=null&&i<moduleList.size();i++){
			 systemModule=(SystemModule)moduleList.get(i);
			 systemModuleTreeNode=(RoleResourceTreeNode)allSystemModuleTreeNode.get(systemModule.getId());
			 parentSystemModuleTreeNode=(RoleResourceTreeNode)allSystemModuleTreeNode.get(systemModule.getParentId());
			 if (parentSystemModuleTreeNode!=null&&systemModuleTreeNode!=null) parentSystemModuleTreeNode.addChildren(systemModuleTreeNode);
		 }
		 
		//查询所有的功能点（资源）
		 SystemResource systemResource=null;
		 Map allSystemResourceTreeNode=new LinkedHashMap();
		List resourceList=systemRoleDao.getHibernateTemplate().find("from SystemResource ");
		 for(int i=0;resourceList!=null&&i<resourceList.size();i++){
			 systemResource=(SystemResource)resourceList.get(i);
			 roleResourceTreeNode=new RoleResourceTreeNode();
			 roleResourceTreeNode.setAuthNodeType(RoleResourceTreeNode.NodeType_funciton);
			 roleResourceTreeNode.setId(systemResource.getId().toString());
			 roleResourceTreeNode.setText(systemResource.getName());
			 roleResourceTreeNode.setLeaf(true);
			 roleResourceTreeNode.setParentId(ObjectUtils.toString(systemResource.getParentId()));
			 allSystemResourceTreeNode.put(systemResource.getId(), roleResourceTreeNode);
			 //设置功能点到模块下
			 systemModuleTreeNode=(RoleResourceTreeNode)allSystemModuleTreeNode.get(systemResource.getModuleId());
			 if (systemModuleTreeNode!=null) systemModuleTreeNode.addChildren(roleResourceTreeNode);
			 
			 //roleResourceTreeNode.addChildren(treeNode);			 			 
		 }
		
		 
		//设置有权限功能点
		 List roleResourceList=systemRoleDao.getHibernateTemplate().find("from SystemRoleAcl t where t.roleId=? ",roleId);
		 SystemRoleAcl systemRoleAcl=null;
		 for(int i=0;roleResourceList!=null&&i<roleResourceList.size();i++){
			 systemRoleAcl=(SystemRoleAcl)roleResourceList.get(i);
			 roleResourceTreeNode=(RoleResourceTreeNode)allSystemResourceTreeNode.get(systemRoleAcl.getResourceId());
			 if (roleResourceTreeNode!=null) {
				 roleResourceTreeNode.setChecked(true);
				 //如果模块或其子模块有一个功能被选择了，该模块也会被选择
				 //设置模块被选 
				 systemResource=SystemRoleResourceCache.getSystemResourceById(systemRoleAcl.getResourceId());
				 if (systemResource!=null){
					 //allSystemModuleTreeNode 模块
					 systemModule=(SystemModule)allSystemModuleMap.get(systemResource.getModuleId());
					 if (systemModule!=null){
						 RoleResourceTreeNode roleResourceModuleTreeNode=null;
						 roleResourceModuleTreeNode=(RoleResourceTreeNode)allSystemModuleTreeNode.get(systemModule.getId());
						 
						 //设置本模块
						 if (roleResourceModuleTreeNode!=null) {
							 //roleResourceModuleTreeNode.setChecked(true); //20130918
							 
						 }
						 
						 setParentSystemModuleTreeNodeChcek(allSystemModuleTreeNode,allSystemModuleMap,systemModule);
						 	 
						 
					 }
					 //
				 }
				 //allSystemModuleTreeNode.get(key)
			 }
		 }
		 
		
		 
		 List nodeLists=new ArrayList();
		 for(Iterator it=allSystemModuleTreeNode.values().iterator();it.hasNext();){
			 RoleResourceTreeNode one=(RoleResourceTreeNode)it.next();
    		 if (StringHelper.isEmpty(one.getParentId())||"0".equals(one.getParentId())) nodeLists.add(one);
    	 }

		return nodeLists;
	}
	private void setParentSystemModuleTreeNodeChcek(Map allSystemModuleTreeNode,Map allSystemModuleMap,SystemModule systemModule){
		RoleResourceTreeNode roleResourceModuleTreeNode=(RoleResourceTreeNode)allSystemModuleTreeNode.get(systemModule.getId());
		 
		 //设置本模块
		 if (roleResourceModuleTreeNode!=null) {
			// roleResourceModuleTreeNode.setChecked(true); //20130918
			 
		 }
//		 //设置父模块
//		  roleResourceModuleTreeNode=(RoleResourceTreeNode)allSystemModuleTreeNode.get(systemModule.getParentId());
//		 if (roleResourceModuleTreeNode!=null) {
//			 roleResourceModuleTreeNode.setChecked(true);							
//		 }
		 SystemModule parentSystemModule=(SystemModule)allSystemModuleMap.get(systemModule.getParentId());
		 if (parentSystemModule!=null) setParentSystemModuleTreeNodeChcek(allSystemModuleTreeNode,allSystemModuleMap,parentSystemModule);
		 
		 
	}
	public void setRoleResource(Integer roleId, String resources) {
		// TODO Auto-generated method stub
		if (StringHelper.isEmpty(resources)) return;
		String[] resourcearray = resources.split(",");
		Integer resourceId=null;
		//删除之前的角色功能权限
		systemRoleDao.getHibernateTemplate().bulkUpdate("delete  from SystemRoleAcl t where t.roleId=? ", roleId);
		systemRoleDao.flush();
		SystemRoleAcl systemRoleAcl=null;
		for (int i = 0; i < resourcearray.length; i++)
		{
			resourceId=StringHelper.parseInteger(resourcearray[i], 0);
			if (resourceId.intValue()==0) continue;
			//systemRoleDao.getHibernateTemplate().save(entity);
			systemRoleAcl=new SystemRoleAcl();
			systemRoleAcl.setRoleId(roleId);
			systemRoleAcl.setResourceId(resourceId);
			systemRoleDao.getHibernateTemplate().save(systemRoleAcl);
			systemRoleDao.flush();
		}	
		
	}
	
}
